# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Maintainer: Francesco Colista <fcolista@alpinelinux.org>
pkgname=lxd
pkgver=4.3
pkgrel=0
pkgdesc="a container hypervisor and a new user experience for LXC"
url="https://linuxcontainers.org/lxd/"
arch="all !mips !mips64"
license="Apache-2.0"
depends="
	acl
	netcat-openbsd
	cgmanager
	squashfs-tools
	rsync
	shadow-uidmap
	lxc
	ip6tables
	dnsmasq
	ca-certificates
	tar
	iproute2
	xz
	"
makedepends="
	lxc-dev
	gettext-dev
	acl-dev
	tcl-dev
	libuv-dev
	eudev-dev
	libcap-dev
	linux-headers

	rsync
	go
	intltool

	libtool
	autoconf
	automake
	patchelf
	"
subpackages="
	$pkgname-scripts:scripts
	$pkgname-bash-completion:bashcomp:noarch
	$pkgname-openrc
	$pkgname-libs
	"
install="$pkgname.pre-install"
options="!check"
source="https://linuxcontainers.org/downloads/$pkgname/$pkgname-$pkgver.tar.gz
	$pkgname.confd
	$pkgname.initd
	lxd-dont-go-get.patch
	add-missing-includes.patch
	"
# avoid conflict with system libsqlite3.so.0 by adding a soname prefix.
# this makes lxd-libs provide so:lxd:libsqlite3.so.0 and lxd depend on
# so:lxd:libsqlite3.so.0.
ldpath="/usr/lib/lxd"
sonameprefix="$pkgname:"

_project="github.com/lxc/lxd"

prepare() {
	export GOPATH="$builddir/_dist"
	default_prepare
	#update_config_guess
	for p in raft sqlite dqlite; do
		cd "$GOPATH"/deps/$p
		autoreconf -i
	done
}

build() {
	export GOPATH="$builddir/_dist"
	export CGO_CFLAGS="-I${GOPATH}/deps/sqlite/ -I${GOPATH}/deps/libco/ -I${GOPATH}/deps/raft/include/ -I${GOPATH}/deps/dqlite/include/"
	export CGO_LDFLAGS="$CGO_LDFLAGS -L${GOPATH}/deps/sqlite/.libs/ -L${GOPATH}/deps/libco/ -L${GOPATH}/deps/raft/.libs -L${GOPATH}/deps/dqlite/.libs/ -lintl"
	export LD_LIBRARY_PATH="$ldpath ${GOPATH}/deps/sqlite/.libs/:${GOPATH}/deps/libco/:${GOPATH}/deps/raft/.libs/:${GOPATH}/deps/dqlite/.libs/"

	cd "$GOPATH"/deps/raft
	PKG_CONFIG_PATH="${GOPATH}/deps/raft/" ./configure \
		--prefix=/usr \
		--libdir=/usr/lib/lxd
	make

	cd "$GOPATH"/deps/libco
	make
	
	cd "$GOPATH"/deps/sqlite
	./configure \
		--prefix=/usr \
		--enable-replication \
		--disable-amalgamation \
		--disable-tcl \
		--libdir=/usr/lib/lxd
	make

	cd "$GOPATH"/deps/dqlite
	PKG_CONFIG_PATH="${GOPATH}/deps/sqlite/:${GOPATH}/deps/libco/:${GOPATH}/deps/raft/" ./configure \
		--prefix=/usr \
		--libdir=/usr/lib/lxd

	make CFLAGS="-I${GOPATH}/deps/sqlite/ -I${GOPATH}/deps/libco/ -I${GOPATH}/deps/raft/include/" LDFLAGS="-L${GOPATH}/deps/sqlite/.libs/ -L${GOPATH}/deps/libco/ -L${GOPATH}/deps/raft/.libs/"

	cd "$builddir"
	go install -v -x -tags libsqlite3 $_project/lxd
	for bin in lxc fuidshift lxc-to-lxd lxd-benchmark lxd-agent lxd-p2c; do
		go install -v -x $_project/$bin
	done
}

package() {
	for r in sqlite dqlite; do
		cd "$GOPATH"/deps/$r
		make DESTDIR="$pkgdir" install
	done

	cp ${GOPATH}/deps/libco/libco.so* "$pkgdir"/usr/lib/lxd
	cp ${GOPATH}/deps/raft/.libs/libraft.so* "$pkgdir"/usr/lib/lxd

	rm $pkgdir/usr/bin/sqlite3
	rm -r $pkgdir/usr/include

	cd ${GOPATH}
	install -Dm755 bin/$pkgname "$pkgdir"/usr/sbin/$pkgname
	install -Dm755 bin/lxc "$pkgdir"/usr/bin/lxc
	
	patchelf --set-rpath "/usr/lib/lxd" "${pkgdir}/usr/sbin/lxd"
	
	install -Dm755 "$srcdir"/lxd.initd \
		"$pkgdir"/etc/init.d/lxd
	install -Dm644 "$srcdir"/lxd.confd \
		"$pkgdir"/etc/conf.d/lxd
}

bashcomp() {
	depends="bash"
	pkgdesc="Bash completions for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"

	cd "$builddir"
	mkdir -p "$subpkgdir"/usr/share/bash-completion/completions
	cp scripts/bash/lxd-client "$subpkgdir"/usr/share/bash-completion/completions/lxd-client
}

scripts() {
	pkgdesc="LXD scripts"
	depends="$pkgname py3-lxc"

	export GOPATH="$builddir/_dist"
	cd $GOPATH

	for tool in fuidshift lxc-to-lxd lxd-benchmark lxd-agent lxd-p2c; do
		install -Dm755 bin/$tool "$subpkgdir"/usr/bin/$tool
	done
	install -Dm755 ../scripts/empty-lxd.sh "$subpkgdir"/usr/bin/empty-lxd.sh
}

libs() {
	pkgdesc="LXD bundled libraries"
	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/lib "$subpkgdir"/usr/
	patchelf --set-rpath "/usr/lib/lxd" "${subpkgdir}/usr/lib/lxd/libdqlite.so"
	patchelf --set-rpath "/usr/lib/lxd" "${subpkgdir}/usr/lib/lxd/libsqlite3.so"
	rm -rf "$subpkgdir"/usr/lib/lxd/pkgconfig \
		"$subpkgdir"/usr/lib/lxd/*.a
	rm -r "$pkgdir"/etc
}

sha512sums="370dd3585ea690cf7f6c42db94e71ee05f81e6931c08bf91d9fc3a6d641565784286d2d05c208b62d3ad72519c2311209943a38d93b7de017e7dc356446b93bf  lxd-4.3.tar.gz
bc32c71f2ce10f508433e1e4651c08c18e8a17e9419a7ce391c0f127fc7cf378c665178926b35eae8813e290d9c5eab3ceb605679fd32efdf2cf98a57cee4127  lxd.confd
94de0c0d5ab63463a929a4151359950b1117d0ada5ccf0944311cc70c6b6d4c437ccb4158734ab35db67bfb4abc437074c3f3515be4531f63adc74da21fefb5b  lxd.initd
6bd42ac2571eb77fc3761f549bf77771fbfc4dad8934f9ccf6e421b2874ae5205345511d46f16f69b0b653e859d8019382c5da077db9cbb8149ae883d544215c  lxd-dont-go-get.patch
c9291e7df7e7d62324d6f31460268b138eaeb7928b2aceb9e1763bc123c4410557a887847e441c03efb92642a6c39c8db6557e2af6a738ac46a9df93b5ac1d3b  add-missing-includes.patch"
